# -*- coding: utf-8 -*-

#比较两种方式在读取数据上的效率
#方式1：普通成批读取，然后用sync同步成等长序列
#方式2：逐个读取，获得的直接就是等长序列. 发现onebyone比成批速度快1.5倍
#经过分析，发现性能瓶颈主要在于organize_tuples2quotes这个函数，因为数据没有按照stock,date排序，所以分派非常耗时
#优化之后，效率相差不多，但还是onebyone快%15左右

from time import time
from django.core.management import setup_environ
import wolfox.foxit.settings as target
setup_environ(target)

from django.db import connection

from wolfox.foxit.dune.store import NormalStore as s

def onebyone(s,ref,stocks,begin,end):
    rev = []
    #i=0
    for stock in stocks:
        #print i,'execute:',stock
        #i+=1
        rev.append(s.get_refbased_xquotes(connection,ref,stock,begin,end))
    return rev

from wolfox.core.task import Task
my_sync = Task.sync

step=30
def batch(s,ref,stocks,begin,end):
    b=time()
    rev = []
    base = s.get_xquotes2(connection,[ref],begin,end)[ref]
    #date_base = [ t.tdate for t in base]
    date_base = [ t[1] for t in base]
    #print date_base
    qs = {}
    for x in xrange(step,len(stocks),step): #一次性导致长时间僵死，所以分批处理
        print 'batch:',x
        qs.update(s.get_xquotes2(connection,stocks[x-step:x],begin,end))
    if x < len(stocks):
        print 'batch end:',x
        qs.update(s.get_xquotes2(connection,stocks[x:],begin,end))
    #print type(qs)
    m=time()
    print 'batch read:',m-b
    #i=0
    for v in qs.values():
        #print 'execute:',i
        #i+=1
        #vdate = [ t.tdate for t in v]
        vdate = [ t[1] for t in v]
        rev.append(my_sync(date_base,vdate,v))
    #print base
    return rev
    
if __name__ == '__main__':
    ns = s()
    id2code = ns.get_id2code()
    code2id = ns.get_code2id()
    print len(code2id)
    #print code2id['SZ000900']

    b1=time()
    #r1=onebyone(ns,1,id2code.keys(),20000000,20030001)
    e1=time()
    print e1-b1

    
    b2=time()
    r2=batch(ns,u'SH000001',code2id.keys(),20000000,20030101)
    e2=time()
    print e2-b2    

    #print len(r1)
    print len(r2)

    from pprint import pprint
    #pprint(r2[100])
    #pprint(r1[-1])

    print e1-b1
    print e2-b2
